package com.wld.xm.framework.core.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by bshao 2018/4/12
 */
public class DateUtil {

    public static String now() {
        LocalDateTime dateTime = LocalDateTime.now();
        return dateTime.toString();
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.now());
    }

    /**
     * 获取当前日期与周一相差的天数
     *
     * @return
     */
    public static int getMondayPlus() {
        Calendar day = Calendar.getInstance();
        int dayOfWeek = day.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) { //一周中第一天（周日）
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    /**
     * 获得当天的起始时间
     *
     * @return
     */
    public static Calendar getStartDate() {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        today.set(Calendar.HOUR_OF_DAY, 0);
        today.set(Calendar.MINUTE, 0);
        today.set(Calendar.SECOND, 0);
        today.set(Calendar.MILLISECOND, 0);
        return today;
    }

    /**
     * 获取当天截止时间
     *
     * @return
     */
    public static Calendar getEndDate() {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        today.set(Calendar.HOUR_OF_DAY, 23);
        today.set(Calendar.MINUTE, 59);
        today.set(Calendar.SECOND, 59);
        today.set(Calendar.MILLISECOND, 59);
        return today;
    }

    public static Date getStartWeekDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        return calendar.getTime();
    }

    /**
     * 获得当月起始时间
     *
     * @return
     */
    public static Date getStartMounth() {
        Calendar calendar = getStartDate();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 获得当月结束时间
     *
     * @return
     */
    public static Calendar getEndMounth(Calendar endToday) {
        Calendar endMounth = getEndDate();
        endMounth.set(Calendar.DAY_OF_MONTH, endMounth.getActualMaximum(endMounth.DAY_OF_MONTH));
        return endMounth;
    }

    /**
     * 获取当前季度 起始时间
     *
     * @return
     */
    public static Date getStartQuarter() {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        int currentMonth = today.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                today.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                today.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                today.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                today.set(Calendar.MONTH, 9);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return today.getTime();
    }

    /**
     * 获取当季的结束时间
     */
    public static Calendar getEndQuarter() {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        int currentMonth = today.get(Calendar.MONTH) + 1;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                today.set(Calendar.MONTH, 2);
                today.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                today.set(Calendar.MONTH, 5);
                today.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                today.set(Calendar.MONTH, 8);
                today.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                today.set(Calendar.MONTH, 11);
                today.set(Calendar.DATE, 31);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return today;
    }

    /**
     * 获取当年起始时间
     */
    public static Calendar getStartYear() {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        try {
            today.set(Calendar.MONTH, 0);
            today.set(Calendar.DAY_OF_MONTH, today.getActualMinimum(Calendar.DAY_OF_MONTH));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return today;
    }

    /**
     * 获取当年结束时间
     */
    public static Calendar getEndYear() {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        try {
            today.set(Calendar.MONTH, 11);
            today.set(Calendar.DAY_OF_MONTH, today.getMaximum(Calendar.DAY_OF_MONTH));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return today;
    }

    /**
     * String 转Date
     *
     * @param strDate
     * @param format
     * @return
     */
    public static Date strToDate(String strDate, String format) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDate, pos);
            return strtodate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static final long ONE_MINUTE = 60;

    private static final long ONE_HOUR = 3600;

    private static final long ONE_DAY = 86400;

    private static final long ONE_MONTH = 2592000;

    private static final long ONE_YEAR = 31104000;

    /**
     * 距离今天多久
     *
     * @param date
     * @return
     */
    public static String fromToday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        long time = date.getTime() / 1000;
        long now = new Date().getTime() / 1000;
        long ago = now - time;
        if (ago <= ONE_HOUR)
            return ago / ONE_MINUTE + "分钟前";
        else if (ago <= ONE_DAY)
            return ago / ONE_HOUR + "小时" +
                    (ago % ONE_HOUR / ONE_MINUTE)
                    + "分钟前";
        else if (ago <= ONE_DAY * 2)
            return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        else if (ago <= ONE_DAY * 3)
            return "前天"
                    + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        else if (ago <= ONE_MONTH) {
            long day = ago / ONE_DAY;
            return day + "天前" +
                    calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        } else if (ago <= ONE_YEAR) {
            long month = ago / ONE_MONTH;
            long day = ago % ONE_MONTH / ONE_DAY;
            return month
                    + "个月" +
                    day + "天前"
                    + calendar.get(Calendar.HOUR_OF_DAY) + "点"
                    + calendar.get(Calendar.MINUTE) + "分";
        } else {
            long year = ago / ONE_YEAR;
            int month = calendar.get(Calendar.MONTH) + 1;
            //JANUARY which is 0 so month +1
            return year
                    + "年前" +
                    month + "月" +
                    calendar.get(Calendar.DATE)
                    + "日";
        }
    }

    /**
     * @desc: 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
     * @return:
     * @auther: xie_lp
     * @date: 2018/8/9 0009 14:33
     */
    public static String dateToStrLong(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * @desc: 将长时间格式时间转换为字符串 yyyy-MM-dd
     * @return:
     * @auther: xie_lp
     * @date: 2018/8/9 0009 14:33
     */
    public static String dateToStr(java.util.Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }
}
